<?php
require_once( CONFIG_DIR.'reg_shop_config.php' );

	$shopDAO = GetShopDAO();
	$errmsg = GetMessageStack();
	
	if(!isset($_GET['sid'])||!is_numeric($_GET['sid'])){
		
		edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));			
	
	}else{
		
		$sid = intval( $_GET['sid'] );
	
		if( !$shopDAO->GetNickName($sid) ){
		  
			edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));		
		
		}	
	}

	if(isset($_POST['action'])&&$_POST['action']=='updateShop'){
		
		$shopName = edp_db_input(trim($_POST['shopName']));
		$logoURL = edp_db_input(trim($_POST['logoURL'])); 	
		$vcode = trim($_POST['vcode']);	
		$shopDesc = $_POST['shopDesc'];	//EdpFilterHtmlTag( edp_db_input(trim($_POST['shopDesc'])) ); 
				
		if( !IsParaOK() ){
			
			if(get_magic_quotes_gpc()){
				 
				$shopDesc =   stripslashes($shopDesc);//显示post后没有quote的在本页面							
			}
		
		    return; 
		}		
		
		if(!edp_isset_session('sid')){
	    	
			$errmsg->add( 'reg', '错误请求,请重新输入' );
		    return; 
		    
	    }else{
	
	    	$sid = edp_getsession('sid');	
	    	
	    	$shopDesc2 = EdpFilterHtmlTag( edp_db_input(trim($shopDesc)) ); 

	    	
	    	//顺便更新淘宝店铺推广链接
	    	if(edp_getsession('platid') == '1'){
	    		
	    		$shopPlusResult=$shopDAO->GetShopInfoPlus('taobao',$sid);	 
	    				
		    	require_once(LIB_DIR.'api/taobao/Taoapi.php');		  			  							
	 			$Taoapi = new Taoapi;
				$Taoapi->method = 'taobao.taobaoke.shops.convert';
				$Taoapi->fields = 'click_url,shop_commission.rate';
				$Taoapi->sids = $shopPlusResult['taobao_id'];
				$Taoapi->pid = TAOBAOKE_ID;
				$TaobaokeData = $Taoapi->Send('get','xml')->getArrayData();									

				if(edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($TaobaokeData))						 
			    {	
			    	$commissionURL = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['click_url'];
			    	$commissionRate = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['shop_commission.rate'];
			    	
			    }else{
			    	
			        $commissionRate = '0';   
				    $commissionURL =  edp_getsession('shopURL');  
			    }	    	

			    if($commissionURL!=$shopPlusResult['click_url']){
			    	
			    	$shopDAO->UpdateTaobaoPlusData($sid,$commissionRate,$commissionURL);
			    	
			    }			    
	    	}
	    	
	    	
	   		$shopDAO->UpdateShopData($sid,GetLoginUserID(),$shopName,$logoURL,$shopDesc2);

	   		edp_unset_session('sid');
			edp_unset_session('platid');
			edp_unset_session('shopURL');
			
			
	    	if(get_magic_quotes_gpc()){
				 
				$shopDesc =   stripslashes($shopDesc);//显示post后没有quote的在本页面						
			}
				   		
			//事件通知
			$para['sid']=$sid;
			$eventDispatcher = GetEventDispatcher();
			$eventDispatcher->Notify( GetLoginUserID(), MODIFY_SHOP, $para);

			
	  		$errmsg->add( 'reg', '信息更新成功', 'success' );	  
	  		return;  	
	    }
		
	}else{
		
		$result = $shopDAO->GetShopInfo($sid);
		
		if(!$result){
		
			TransferTo('not_allow_update_shop');
			return;	
		}			
		
		$shopName = $result['shop_name'];
		$logoURL = $result['logo_url']; 	
		$shopDesc = $result['description'];
		$platID = $result['plat_id'];		
		$shopURL = $result['shop_url'];		
			
		if(GetLoginUserID()==$result['modify_user_id']){
			
			//在一天之内只能更新一次信息
			if(EdpSubStr($result['modify_time'],0,10)==GetCurrentDate( 'Y-m-j' )){
				
				TransferTo('not_allow_update_shop_inoneday');
				return;	
			}
					
		}		
		
		edp_setsession('sid',$sid);
		edp_setsession('platid',$platID);	
		edp_setsession('shopURL',$shopURL);	
		
	}


?>
